package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserSuggest;
import com.qqtech.qquan.user.model.UserSuggestQuery;
import com.qqtech.qquan.user.model.UserSuggestReplyQuery;
import com.qqtech.qquan.user.model.UserSuggestReplyVo;
import com.qqtech.qquan.user.model.UserSuggestVo;
import com.qqtech.qquan.user.service.UserSuggestReplyService;
import com.qqtech.qquan.user.service.UserSuggestService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/suggest")
public class UserSuggestController extends BaseWebController<UserSuggest, UserSuggestQuery> {
	@Resource
	private UserSuggestService userSuggestService;
	
	@Resource
	private UserSuggestReplyService userSuggestReplyService;
    
	@Override
	protected BaseService<UserSuggest> getBaseService() {
		return userSuggestService;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserSuggestVo userSuggestVo) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		userSuggestVo.setCreatorId(curUserId);
		userSuggestVo.setIsReply(YesNoEnum.NO.getCode());
		userSuggestVo.setStatus(YesNoEnum.YES.getCode());
		userSuggestService.insert(userSuggestVo);
		return ret;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody UserSuggestVo userSuggestVo) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		UserSuggestVo userSuggestVo2 = userSuggestService.getById(userSuggestVo.getId());
		if (null != userSuggestVo2&&userSuggestVo2.getCreatorId()==curUserId.intValue()) {
			userSuggestVo2.setStatus(YesNoEnum.NO.getCode());
			userSuggestService.updateById(userSuggestVo2);
		}
		return ret;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/managePage")
	@ResponseBody
	public Result managePage(@RequestBody UserSuggestQuery userSuggestQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		userSuggestQuery.setCreatorId(curUserId);
		userSuggestQuery.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(userSuggestQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserSuggestVo> page = userSuggestService.queryPageList(userSuggestQuery, pageKit);
		for (UserSuggestVo userSuggestVo : page) {
			userSuggestVo.setImgString(userSuggestVo.getImg(),SysConst.IMG_POSTFIX);
		}
		result.put("page", page);
		return result;
	}
	
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody UserSuggestQuery userSuggestQuery) {
		Result result = new Result();
		UserSuggestVo detailVo = userSuggestService.getById(userSuggestQuery.getId());
		detailVo.setImgString(detailVo.getImg(),SysConst.IMG_POSTFIX);
		if(YesNoEnum.YES.getCode()==detailVo.getIsReply()){
			UserSuggestReplyQuery userSuggestReplyQuery=new UserSuggestReplyQuery();
			userSuggestReplyQuery.setSuggestId(userSuggestQuery.getId());
			List<UserSuggestReplyVo> replys=userSuggestReplyService.queryList(userSuggestReplyQuery);
			if(null!=replys&&replys.size()>0){
				UserSuggestReplyVo replyDetail=replys.get(0);
				replyDetail.setImgString(replyDetail.getImg(),SysConst.IMG_POSTFIX);
				result.put("replyDetail", replyDetail);
			}
		}
		result.put("detailVo", detailVo);
		return result;
	}
}
